package com.me.mvc.controller.transactions;

import java.util.LinkedList;
import java.util.List;

import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.me.mvc.model.ModelUtils;
import com.me.mvc.model.entity.Transaction;
import com.me.mvc.model.entity.representation.xml.EntityList;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Roman Uholnikov
 */

@RestController
@RequestMapping("/transactions")
public class TransactinsRest {


    @RequestMapping(value = "/byClient/{clientId}/rest.json", method = RequestMethod.GET)
    @Produces({MediaType.APPLICATION_JSON})
    public List<Transaction> showByClient(ModelMap model, @PathVariable("clientId") Integer clientId) {
        List<Transaction> transactionList = new LinkedList<Transaction>();
        for (Transaction transaction : ModelUtils.transactionMap.values()) {

            if ((transaction.getReceiverAccount().getClient().getId() == clientId)
                    || (transaction.getSenderAccount().getClient().getId() == clientId)) {
                transactionList.add(transaction);
            }
        }
        return transactionList;
    }

    @RequestMapping(value = "/byClient/{clientId}/rest.xml", method = RequestMethod.GET)
    @Produces({MediaType.APPLICATION_XML})
    public EntityList<Transaction> showByClientXml(ModelMap model, @PathVariable("clientId") Integer clientId) {
        List<Transaction> transactionList = new LinkedList<Transaction>();
        for (Transaction transaction : ModelUtils.transactionMap.values()) {

            if ((transaction.getReceiverAccount().getClient().getId() == clientId)
                    || (transaction.getSenderAccount().getClient().getId() == clientId)) {
                transactionList.add(transaction);
            }
        }
        return new EntityList<Transaction>(transactionList);
    }

    @RequestMapping(value = "/rest.xml", method = RequestMethod.GET)
    @Produces({MediaType.APPLICATION_XML})
    public EntityList<Transaction> all(ModelMap model) {
        return new EntityList<Transaction>(new LinkedList<Transaction>(ModelUtils.transactionMap.values()));
    }

    @RequestMapping(value = "/rest.json", method = RequestMethod.GET)
    @Produces({MediaType.APPLICATION_JSON})
    public List<Transaction> allJson(ModelMap model) {
        return new LinkedList<Transaction>(ModelUtils.transactionMap.values());
    }


}
